package com.llb666.common.core.exception;


import com.llb666.common.core.web.error.ErrorCode;

import java.text.MessageFormat;

/**
 * 全局统一异常.
 *
 * @author llb666
 */
public class AppException extends RuntimeException {

    /**
     * 异常错误码
     */
    private final ErrorCode errCode;

    /**
     * 异常错误消息
     */
    private String msg;

    public AppException(ErrorCode errCode) {
        this.errCode = errCode;
    }

    public AppException(ErrorCode errCode, String... param) {
        this.msg = MessageFormat.format(errCode.getMsg(), param);
        this.errCode = errCode;
    }

    public AppException withMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public ErrorCode getErrCode() {
        return errCode;
    }

    public Integer getCode() {
        return errCode.getCode();
    }

    public String getMsg() {
        if (this.msg != null) {
            return this.msg;
        }
        return errCode.getMsg();
    }

}
